/**
 * 数据表管理类
 * Created By WangZhiHao
 */
class TableManager {

    private static _instance: TableManager;
    public static get instance(): TableManager {
        if (!this._instance) {
            this._instance = new TableManager();
        }
        return this._instance;
    }

    private _txtTables: Object = {};

    public get txtTabels(): Object {
        return this._txtTables;
    }

    /**解析数据表 */
    public parseTable(callback: Function): void {
        cc.resources.load("table", (err, content: cc.Asset) => {
            var xhr = new XMLHttpRequest();
            xhr.responseType = "arraybuffer";
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4) {
                    var status = xhr.status;
                    if (status >= 200 && status < 300) {
                        // console.log(xhr.response);
                        if (xhr.response instanceof ArrayBuffer) {
                            let data = new Uint8Array(xhr.response);
                            var zip = new JSZip(data);
                            var zipFiles: any = zip["files"];
                            for (var key in zipFiles) {
                                var name: string = key.substr(0, key.indexOf("."));
                                var info: string = zipFiles[key].asText();
                                TableManager.instance.registerTxtTables(name, info);
                            }
                            cc.resources.release("table");
                            callback();
                        }
                    } else {
                        console.log(status);
                    }
                }
            };
            xhr.open("GET", content.nativeUrl, true);
            // xhr.setRequestHeader("Content-Type", "application/json");
            xhr.send(null);
        });
    }

    /**
     * 注册表数据
     */
    public registerTxtTables(name: string, text: string): void {
        if (!name || name == "") return console.error("name is null");

        var clazz: any = window[name];//egret.getDefinitionByName(name);
        if (clazz == null) {
            return;
        }

        var dict: Object = this._txtTables[name];
        if (!dict || dict == null) {
            dict = this._txtTables[name] = {};
        }

        var tableLines: Array<string> = text.split("\r\n");
        var length: number = tableLines.length;
        for (var i = 0; i < length; i++) {
            var line: string = tableLines[i];
            if (line == "") {
                // console.error("TableManager.registerTxtTables Error:Table " + name + " line " + (i + 1) + " is empty!");
                continue;
            }
            var table: ITxtTable = new clazz();
            var id: any = table.fillData(line.split("\t"));
            dict[id] = table;
        }
    }

    public unregisterTxtTables(name: string, id: any): void {
        var dict: Object = this._txtTables[name];
        if (dict != null) {
            delete dict[id];
        }
    }

    /**
     * 获取所有数据
     * @param name 
     * @returns 
     */
    public getTxtTables(name: string): Object {
        return this._txtTables[name];
    }


    /**
     * 获取Txt数据表
     */
    public getTxtTable(name: string, id: any): any {//ITxtTable
        var dict: Object = this.getTxtTables(name);
        if (dict != null) {
            return dict[id];
        }
        return null;
    }
}
// /**
//  * 获取单个数据
//  * @param name 
//  * @param id 
//  * @returns 
//  */
// function getTable(name: string, id: any): any {
//     return TableManager.instance.getTxtTable(name, id);
// }
// /**
//  * 获取所有数据
//  * @param name 
//  * @returns 
//  */
// function getTables(name): Object {
//     return TableManager.instance.getTxtTables(name);
// }

window["TableManager"] = TableManager;